package cb.com.etcdemo;

import android.os.Bundle;
import android.support.annotation.NonNull;

import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import cb.com.etcdemohgz.R;

public class MainActivity extends BaseActivity {

    private TextView mTextMessage;
    private FragmentManager FM;
    private FragmentTransaction MfragmentTransaction;
    private OnLineFragment onLineFragment;
    private OfflineFragment offlineFragment;
    private NotOnLineFragment notOnLineFragment;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    //2.开启一个事务，通过调用beginTransaction方法开启。
                    MfragmentTransaction = FM.beginTransaction();
                    MfragmentTransaction.show(onLineFragment);
                    MfragmentTransaction.hide(offlineFragment);
                    MfragmentTransaction.hide(notOnLineFragment);
                    MfragmentTransaction.commit();
                    onLineFragment.setUserVisibleHint(true);
                    return true;
                case R.id.navigation_dashboard:
                    //2.开启一个事务，通过调用beginTransaction方法开启。
                    MfragmentTransaction = FM.beginTransaction();
                    MfragmentTransaction.show(offlineFragment);
                    MfragmentTransaction.hide(onLineFragment);
                    MfragmentTransaction.hide(notOnLineFragment);
                    MfragmentTransaction.commit();
                    offlineFragment.setUserVisibleHint(true);
                    return true;
                case R.id.navigation_notifications:
                    //2.开启一个事务，通过调用beginTransaction方法开启。
                    MfragmentTransaction = FM.beginTransaction();
                    MfragmentTransaction.show(notOnLineFragment);
                    MfragmentTransaction.hide(offlineFragment);
                    MfragmentTransaction.hide(onLineFragment);
                    MfragmentTransaction.commit();
                    notOnLineFragment.setUserVisibleHint(true);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        //获取到FragmentManager，在V4包中通过getSupportFragmentManager，
        //在系统中原生的Fragment是通过getFragmentManager获得的。
        FM = getSupportFragmentManager();
        //2.开启一个事务，通过调用beginTransaction方法开启。
        MfragmentTransaction = FM.beginTransaction();
        //把自己创建好的fragment创建一个对象
        onLineFragment = new OnLineFragment();
        offlineFragment = new OfflineFragment();
        notOnLineFragment = new NotOnLineFragment();
        //向容器内加入Fragment，一般使用add或者replace方法实现，需要传入容器的id和Fragment的实例。
        //提交事务，调用commit方法提交。
        MfragmentTransaction.commit();

    }
}
